package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Integral;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService commendService;
    @Autowired
    private ICollectService collectService;

    @PostMapping("")
    public Object save(Travel t){
        JSONResult json = new JSONResult();
        try {
            t.setAuthor(UserContext.getCurrentUser());
            travelService.save(t);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @GetMapping("")
    public Object selectByTravelId(Long id){
        Travel travel = travelService.selectOne(id);
        TravelContent content = travelService.selectTravelContentById(id);
        travel.setContent(content);
        return travel;
    }

    @PutMapping("")
    public Object updata(Travel travel){
        JSONResult json = new JSONResult();
        try {
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @GetMapping("/travel")
    public Object list(TravelQueryObject qo){
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_RELEASE);
        if(qo.getOrderBy()==null){
            qo.setOrderBy("t.releaseTime desc");
        }
        return travelService.query(qo);
    }

    @GetMapping("/selectCommend")
    public Object selectCommend(TravelCommendQueryObject qo){

        return commendService.selectForCommend(qo);
    }


    @PostMapping("/{userId}")
    public Object addCollect(CollectQueryObject qo){
        JSONResult json = new JSONResult();
        try {
            collectService.addTravelCollect(qo);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @DeleteMapping("/{userId}")
    public Object removeCollect(CollectQueryObject qo){
        JSONResult json = new JSONResult();
        try {
            collectService.removeTravelCollect(qo);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @GetMapping("/collectCount")
    public Object countCollect(Long travelId){
        Integer num = collectService.countTravelCollect(travelId);
        return num;
    }

    @GetMapping("/{userId}/{travelId}")
    public Object isCollect(@PathVariable Long userId,@PathVariable Long travelId){
        Integer count = collectService.travelIsCollect(userId,travelId);
        return count!=0;
    }

    @GetMapping("/travelCollect")
    public Object selectCollectByUserId(Long userId){
       return collectService.selectCollectByUserId(userId);
    }

    //浏览数更新
    @PutMapping("/viewCount")
    public Object updateViewCount(Long travelId){
        travelService.updateViewCount(travelId);
        return null;
    }

    //查询有几篇游记
    @GetMapping("/travelCount")
    public Object selectTravelCount(Long authorId){
        Integer count = travelService.selectTravelCount(authorId);
        return count;
    }
}
